import { getCurrentInstance, ref } from 'vue';
import router from '../router/index';

export default (await import('vue')).defineComponent({
setup() {

const { proxy } = getCurrentInstance();
const username = ref('');
const password = ref('');
const password2 = ref('');

const validatorPassword = (val) => /\d{6}/.test(val);

// 校验函数可以直接返回一段错误提示
const validatorPassword2 = (val) => {
console.log(val);
if (val !== password.value) {
return false;
} else {
return true;
}
};

const onRegister = () => {
if (username.value === '' || password.value === '' || password2.value === '' || password.value != password2.value) {
proxy.$toast.fail('请按照规范输入');
return;
}
const data = {
username: username.value,
password: password.value,
password2: password2.value
};
proxy.$axios.post('/register', data).then(res => {
console.log(res);
proxy.Stoast.success('注册成功');
router.push('/login');
});
};
const goLogin = () => {
router.push('/login');
};

return {
username,
password,
password2,
onRegister, goLogin,
validatorPassword,
validatorPassword2
};
},
});
